<!DOCTYPE html>
<html>

<meta http-equiv="content-type" content="text/html;charset=UTF-8" />

<head>
  <meta charset="utf-8">
  <title>layuiAdmin 角色管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>

<body>

  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <div class="">
              <input type="text" name="Query" placeholder="请输入角色名" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-admin" lay-submit="" lay-filter="search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
          </div>
        </div>
      </div>
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <div class="layui-btn layuiadmin-btn-role" id="add" lay-event="add">添加</div>
        </div>

        <table id="lay-tb" lay-filter="lay-tb"></table>
        <script type="text/html" id="buttonTpl">
          {{#  if(d.check == true){ }}
            <button class="layui-btn layui-btn-xs">已审核</button>
          {{#  } else { }}
            <button class="layui-btn layui-btn-primary layui-btn-xs">未审核</button>
          {{#  } }}
        </script>
      </div>
    </div>
  </div>
  <script type="text/html" id="cdhtml">
    {{# if(d.Cd.filter(r=>r.parentId!=0).map(r=>r.Name).toString() != '') { }}
        <a  class="layui-table-link">{{d.Cd.filter(r=>r.parentId!=0).map(r=>r.Name).toString()}}</a>
    {{# } else { }}
        <a style="color:#FF5722" >暂无操作权限</a>
    {{#  } }}
  </script>
  <script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑权限</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  </script>
  <script src="../../../layuiadmin/layui/layui.js"></script>
  <script>
    layui.config({
      base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
      index: 'lib/index', //主入口模块
      utilJs: '../lib/extend/utilJs',
      _role: '../lib/_role'
    }).use(['index', 'table', 'utilJs', '_role'], function () {
      var $ = layui.$,
        form = layui.form,
        utilJs = layui.utilJs,
        _role = layui._role,
        table = layui.table;
      let load = utilJs.admin.loading()
      table.render({
        elem: '#lay-tb',
        headers: {
          Token: localStorage.getItem('Token')
        },
        url: `${utilJs.api}HLW01/GetAllRoles` //数据接口
          ,
        page: true //开启分页
          ,
        cols: [
          [ //表头
            {
              type: 'checkbox',
              fixed: 'left'
            }, {
              field: 'Id',
              title: '角色主键',
              width: 150,
              sort: true,
            }, {
              field: 'Name',
              title: '角色名',
              width: 200
            }, {
              field: 'CreateTime',
              title: '创建时间',
              width: 200,
              sort: true
            }, {
              field: 'Cd',
              title: '菜单',
              templet: '#cdhtml'
            }, {
              fixed: 'right',
              width: 150,
              title: '操作',
              align: 'center',
              toolbar: '#barDemo'
            } //这里的toolbar值是模板元素的选择器
          ]
        ],
        done: () => {
          localStorage.removeItem('guid')
          layer.close(load)
        }
      });

      //监听工具条
      table.on('tool(lay-tb)', function (obj) {
        var data = obj.data;
        if (obj.event == 'edit') {
          utilJs.admin
            .prop('./edit_role.html?ids=' + data.Cd.map(r => r.Id) + '&roleid=' + data.Id, '权限配置', 1, r => {
              let guid = localStorage.getItem('guid');
              if (guid == null)
                return false;
              table.reload('lay-tb', {
                where: {} //设定异步数据接口的额外参数
                //,height: 300
              });
            })
        } else if (obj.event == 'del') {
          let id = data.Id;
          utilJs.admin.confim('删除后不可恢复哦！', cr => {
            _role.delete_role(id).then(r => {
              obj.del()
              utilJs.admin.topMsg('操作成功', () => {
              })
            })
          })

        }
      });
      $("#add").click(r => {
        utilJs.admin
          .prop('./add_role.html', '添加角色', ["300px", '400px'], r => {
            let guid = localStorage.getItem('guid');
            if (guid == null)
              return false;
            table.reload('lay-tb', {
              where: {} //设定异步数据接口的额外参数
              //,height: 300
            });
          });
      })
      form.on('submit(search)', r => {
        let Query = r.field.Query;
        table.reload('lay-tb', {
          where: {
            Query
          } //设定异步数据接口的额外参数
          //,height: 300
        });
        return false;
      })




    });
  </script>
</body>

</html>